package com.example.socketdemo.startlistener;

import com.example.socketdemo.buffer.two.SocketThread;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

@Component
public class SpringListener implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        try {
            //1. 定义服务器端口注册
            ServerSocket serverSocket = new ServerSocket(9999);
            System.out.println("服务器已开启");

            while(true){
                //2. 开始在这里暂停等待接收客户端的连接,得到一个端到端的Socket管道
                Socket socket = serverSocket.accept();
                // 开启线程
                new SocketThread(socket).start();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
